package com.tax.controller;

import com.tax.config.response.Response;
import com.tax.dto.req.CompanyBasicReq;
import com.tax.dto.req.TaxCheckReq;
import com.tax.entity.TdGovCompanyBasic;
import com.tax.entity.TdGovCompanyMobilephone;
import com.tax.entity.TdReportRecord;
import com.tax.enums.ReportStateEnum;
import com.tax.service.ITdReportRecordService;
import com.tax.service.TdGovCompanyBasicService;
import com.tax.service.TdGovCompanyMobilephoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "企业")
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private TdGovCompanyBasicService basicService;

    @Autowired
    private TdGovCompanyMobilephoneService mobilephoneService;

    @Autowired
    private ITdReportRecordService reportRecordService;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @ApiOperation(value="获取企业信息",notes = "企业",response = TdGovCompanyBasic.class)
    @RequestMapping(path = "/getInfo",method= RequestMethod.POST)
    public Response<TdGovCompanyBasic> getCompanyInfo(
            @Validated @RequestBody CompanyBasicReq req){
        TdGovCompanyBasic entity = new TdGovCompanyBasic();
        if (StringUtils.isNotBlank(req.getEntName())) {
            entity.setEntName(req.getEntName());
        }
        if (StringUtils.isNotBlank(req.getTaxNo())) {
            entity.setTaxNo(req.getTaxNo());
        }
        TdGovCompanyBasic tdGovCompanyBasic=basicService.getConpanyInfo(entity);
        return new Response<>(tdGovCompanyBasic);
    }

    @ApiOperation(value="根据信用代码获取相关人员手机号",notes = "企业")
    @RequestMapping(path = "/getPhone",method= RequestMethod.GET)
    public Response<List<TdGovCompanyMobilephone>> getPhone(
            @ApiParam(name = "unifiedCode", value = "统一社会信用代码", required = true) @RequestParam(value = "unifiedCode", required = true) String unifiedCode,
            @ApiParam(name = "phone", value = "手机号", required = false) @RequestParam(value = "phone", required = false) String phone
    ) {
        TdGovCompanyMobilephone entity = new TdGovCompanyMobilephone();
        if (StringUtils.isNotBlank(unifiedCode)) {
            entity.setUnifiedCode(unifiedCode);
        }
        if (StringUtils.isNotBlank(phone)) {
            entity.setValue(phone);
        }
        List<TdGovCompanyMobilephone>  entityList = mobilephoneService.getLegalPhone(entity);
        return new Response<>(entityList);
    }

    @ApiOperation(value="获取验证码（法人）",notes = "企业")
    @RequestMapping(path = "/getLegalPhoneValidcode",method= RequestMethod.GET)
    public Response getLegalPhoneCalidcode(
            @ApiParam(name = "phone", value = "手机号", required = true) @RequestParam(value = "phone", required = true) String phone
    ){

        return new Response<>();
    }

    @ApiOperation(value="校验法人手机验证码",notes = "企业")
    @RequestMapping(path = "/CheckLegalPhoneValidcode",method= RequestMethod.GET)
    public Response getLegalPhoneCalidcode(
            @ApiParam(name = "phone", value = "手机号", required = true) @RequestParam(value = "phone", required = true) String phone,
            @ApiParam(name = "validcode", value = "验证码", required = true) @RequestParam(value = "validcode", required = true) String validcode
    ){

        return new Response<>();
    }

    @ApiOperation(value="税务局校验",notes = "企业")
    @RequestMapping(path = "/taxCheck",method= RequestMethod.POST)
    public Response taxCheck(@Validated @RequestBody TaxCheckReq req) {
        //校验税务局账号密码

        return new Response();
    }

    @ApiOperation(value="生成报告",notes = "企业")
    @RequestMapping(path = "/doReport",method= RequestMethod.GET)
    public Response doReport(
            @ApiParam(name = "userId", value = "用户ID", required = true) @RequestParam(value = "userId", required = true) String userId,
            @ApiParam(name = "unifiedCode", value = "统一社会信用代码", required = true) @RequestParam(value = "unifiedCode", required = true) String unifiedCode
    ) {
        TdReportRecord entity = new TdReportRecord();
        entity.setUserId(userId);
        entity.setUnifiedCode(unifiedCode);
        entity.setState(ReportStateEnum.REPORT_STATE_0.getCode());
        entity.setCreatedDate(sdf.format(new Date()));
        reportRecordService.doReport(entity);
        /*TdReportRecord entity = new TdReportRecord();
        entity.setUnifiedCode(unifiedCode);
        entity.setState(ReportStateEnum.REPORT_STATE_0.getCode());
        reportRecordService.insertReportEntity(entity);*/
        return new Response();
    }

    @ApiOperation(value="报告列表",notes = "企业")
    @RequestMapping(path = "/getReportList",method= RequestMethod.GET)
    public Response<List<TdReportRecord>> getReportList(
            @ApiParam(name = "userId", value = "用户ID", required = true) @RequestParam(value = "userId", required = true) String userId,
            @ApiParam(name = "state", value = "状态", required = false) @RequestParam(value = "state", required = false) Integer state
    ) {
        TdReportRecord entity = new TdReportRecord();
        entity.setUserId(userId);
        if (null != state) {
            entity.setState(state);
        }
        List<TdReportRecord> list = reportRecordService.getReportList(entity);
        return new Response(list);
    }

}
